package com.ruoyi.forum.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.forum.dao.CircleTopicDao;
import com.ruoyi.forum.dao.CircleTopicReportDao;
import com.ruoyi.forum.domain.CircleTopic;
import com.ruoyi.forum.domain.CircleTopicReport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description:论坛举报记录表(CircleTopicReport)表服务层
 * @Author: hoda
 * @Date: Create in 2019-09-03
 * @Modified By:
 * @Modified Date:
 */
@Service
public class CircleTopicReportService {

    @Autowired
    private CircleTopicReportDao circleTopicReportDao;
    @Autowired
    private CircleTopicDao circleTopicDao;

    /**
     * 保存举报内容
     *
     * @param topic
     * @param uid
     * @param content
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveReport(CircleTopic topic, String uid, String content) {
        CircleTopicReport report = new CircleTopicReport();
        report.setTopicId(topic.getId());
        report.setContent(content);
        report.setCreateBy(uid);
        report.setCreateTime(System.currentTimeMillis());
        circleTopicReportDao.save(report);

        topic.setReportNum(topic.getReportNum() + 1);
        circleTopicDao.updateById(topic);
    }

    /**
     * 检查帖子是否已被举报
     *
     * @param topicId
     * @param uid
     */
    public Boolean existReport(Long topicId, String uid) {
        // 使用 LambdaQueryWrapper 构造查询条件
        LambdaQueryWrapper<CircleTopicReport> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CircleTopicReport::getTopicId, topicId)
                .eq(CircleTopicReport::getCreateBy, uid);

        // 执行计数查询并判断是否存在
        return circleTopicReportDao.count(wrapper) > 0;

    }

    public Page<CircleTopicReport> page(Page<CircleTopicReport> page, LambdaQueryWrapper<CircleTopicReport> wrapper) {
        return circleTopicReportDao.page(page, wrapper);
    }
}